<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" import="Connect.*"%>
<%@ page import="java.util.ArrayList"%>
<%
	String auction_id = request.getParameter("auction_id");
	String item_id = request.getParameter("item_id");
	String item_name = request.getParameter("item_name");
	session.setAttribute("item_id", item_id);

	// Getting information and updating the database.
	java.sql.Connection conn = null;
	try {
		// Connect to the database.
		Class.forName(ConnectionInfo.myJDBCDriver()).newInstance();
		java.util.Properties sysprops = System.getProperties();
		sysprops.put("user", ConnectionInfo.myUserID());
		sysprops.put("password", ConnectionInfo.myPassword());
		conn = java.sql.DriverManager.getConnection(ConnectionInfo.myURL(), sysprops);

		// Get the bidding information.
		ArrayList<String> username = new ArrayList<String>();
		ArrayList<String> time = new ArrayList<String>();
		ArrayList<String> price = new ArrayList<String>();
		java.sql.Statement statement = conn.createStatement();
		String previous_username = "";
		String previous_time = "";
		double previous_price = 0.0;
		java.sql.ResultSet rs = statement.executeQuery("SELECT minimum_bid, open_date, bid_increment FROM auctions WHERE id='" + auction_id
				+ "'");
		double bid_increment = 0.0;
		if (rs.next()) {
			username.add("Starting Bid");
			time.add(rs.getString("open_date"));
			price.add(rs.getString("minimum_bid"));
			bid_increment = Double.parseDouble(rs.getString("bid_increment"));

			previous_username = username.get(0);
			previous_time = time.get(0);
			previous_price = Double.parseDouble(price.get(0));
		}
		rs = statement.executeQuery("SELECT * FROM bids WHERE auction_id='" + auction_id + "' ORDER BY bid_time ASC");
		while (rs.next()) {
			/*out.println(rs.getString("minimum_bid"));
			out.println(rs.getString("auction_id") + "," + rs.getString("bidder_username") + "," + rs.getString("bid_time") + ","
					+ rs.getString("bid_price") + "<br />");*/
			// Get newer bid information.
			double new_bid_price = Double.parseDouble(rs.getString("bid_price"));
			String new_bid_time = rs.getString("bid_time");
			String new_bidder = rs.getString("bidder_username");

			// Get previous bid information.
			int last_index = username.size() - 1;
			//String old_bidder = username.get(last_index);
			//int old_bid_price = Double.parseDouble(price.get(last_index));
			//String old_bid_time = time.get(last_index);

			// Input new entries.
			if (username.size() == 1) {
				// The only entry is the starting bid.
				username.add(new_bidder);
				time.add(new_bid_time);
				price.add(price.get(0));
				
				previous_username = new_bidder;
				previous_price = new_bid_price;
				previous_time = new_bid_time;
			} else if (previous_price > new_bid_price) {
				// New bid is less than the maximum of a previous bidder.
				username.add(new_bidder);
				time.add(new_bid_time);
				price.add("" + new_bid_price);

				username.add(previous_username);
				time.add(new_bid_time);
				price.add("" + new_bid_price);

				//previous_username = previous_username;
				previous_time = new_bid_time;
				if (new_bid_price > previous_price) {
					previous_price = new_bid_price;//previous_price = previous_price + bid_increment;
				}
			} else if (previous_price < new_bid_price) {
				// New bid price is higher than the maximum of a previous bidder.
				price.set(last_index, "" + previous_price);

				username.add(new_bidder);
				time.add(new_bid_time);
				price.add("" + ConnectionInfo.twoSigFigs(previous_price + bid_increment));

				previous_username = new_bidder;
				previous_time = new_bid_time;
				if (new_bid_price > previous_price) {
					previous_price = new_bid_price;//previous_price = previous_price + bid_increment;
				}
			} else if (previous_price == new_bid_price) {
				// New bid is the same as the maximum of a previous bidder. Old bidder gets priority.
				username.add(new_bidder);
				time.add(new_bid_time);
				price.add("" + previous_price);

				username.add(previous_username);
				time.add(new_bid_time);
				price.add("" + previous_price);

				//previous_username = previous_username;
				previous_time = new_bid_time;
				if (new_bid_price > previous_price) {
					previous_price = new_bid_price;//previous_price = previous_price + bid_increment;
				}
			}
		}
		// Format price strings.
		for (int i = 0; i < price.size(); i++) {
			int length = price.get(i).length();
			int index = price.get(i).indexOf('.');
			while (index > length - 3) {
				price.set(i, price.get(i) + "0");
				length = price.get(i).length();
			}
			if (index < length - 3) {
				price.set(i, price.get(i).substring(0, index + 3));
			}
		}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
<title>Hammer Time Auctions</title>
</head>
<body>
	<div id="content">
		<%@include file="header.jsp"%>
		<h1>
			<%=item_name%>'s Bid History (Auction ID:
			<%=auction_id%>)
		</h1>
		<hr></hr>
		<%
			// Create a back button.
				out.println("<a href=\"view_auctions.jsp\" class=\"margin-5px button\">Back</a>");
		%>
		<div class="sidebar center">
			<table class="data-table">
				<tr>
					<th>Username</th>
					<th>Bid</th>
					<th>Time</th>
				</tr>
				<%
					for (int i = 0; i < username.size(); i++) {
				%>
				<tr>
					<td><%=username.get(i)%></td>
					<td><%=price.get(i)%></td>
					<td><%=time.get(i)%></td>
				</tr>
				<%
					}
				%>
			</table>
		</div>
	</div>
	<hr></hr>
	<%@include file="footer.jsp"%>
</body>
</html>
<%
	} catch (Exception e) {
		e.printStackTrace();
		out.print(e.toString());
	} finally {
		try {
			conn.close();
		} catch (Exception ee) {
		}
	}
%>